package com.activenetwork.util.datepicker;

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.How;

import com.activenetwork.smlite.page.generic.PageObject;
import com.activenetwork.util.PageLoader;
import com.activenetwork.util.WaitUtil;

public class CalendarYearDiv implements PageObject {
	private WaitUtil waitUtil = new WaitUtil(500);
	
	@FindBy(how = How.CSS, using = ".datepicker.dropdown-menu[style*='display: block;'] div.datepicker-years")
	private WebElement div;
	
	public CalendarMonthDiv pick(int year){
		while(true){
			List<WebElement> yearList = div.findElements(By.xpath(".//span[contains(@class, 'year')]"));
			int firstYear = Integer.valueOf(yearList.get(0).getText());
			int lastYear = Integer.valueOf(yearList.get(11).getText());
			if(year <= lastYear && year >= firstYear){
				break;
			}else if(year > lastYear){
				div.findElement(By.xpath(".//i[@class='icon-arrow-right']")).click();
				waitUtil.waiting();
			}else if(year < firstYear){
				div.findElement(By.xpath(".//i[@class='icon-arrow-left']")).click();
				waitUtil.waiting();
			}
		}
		
		div.findElement(By.xpath(".//span[text()='" + year + "']")).click();
		waitUtil.waiting();
		return PageLoader.load(CalendarMonthDiv.class);
	}
	

}
